package com.k.kpp.web.controller;

import com.k.kc.util.DateUtils;
import com.k.kpp.config.ftp.FtpClientProperties;
import com.k.kpp.config.ftp.FtpClientTemplate;
import com.k.kpp.model.entity.User;
import com.k.kpp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author LJW
 */
@Controller
public class HelloController {
    @Autowired
    private FtpClientProperties ftpClientProperties;

    @Autowired
    private FtpClientTemplate ftpClientTemplate;

    @Autowired
    private ExecutorService executorService;

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private UserService userServiceImpl;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        System.out.println(DateUtils.now());
        executorService.execute(new MyTask("tom"));
        executorService.execute(new MyTask("jim"));
       // executorService.submit(new MyTaskCallable("kim"));
        /*executorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("run");
            }
        });
        executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                String result = "ret value";
                return result;
            }
        });*/
        return String.format("Hello %s! %s", name,com.k.kc.util.DateUtils.now());
    }
    @RequestMapping("/test111")
    @ResponseBody
    public ResponseEntity<User> testResponseEntity(){
        User u = new User(1,"ljw","ljw@a.com");
        System.out.println(1/0);
        ResponseEntity<User> r = new ResponseEntity<User>(u, HttpStatus.valueOf(200));
        return r;
    }
    static class MyTaskCallable implements Callable<String>{
        private String name;
        MyTaskCallable(String name){
            this.name = name;
        }
        @Override
        public String call() throws Exception {
            return "hello"+name;
        }
    }
    static class MyTask implements Runnable{
        private String param;
        MyTask(String param){
            this.param = param;
        }
        @Override
        public void run() {
            try {
                Thread.sleep(5000L);
                System.out.println(DateUtils.now()+Thread.currentThread()+":mytask:"+param);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/testGetConfig")
    @ResponseBody
    public String testGetConfig(){
        String config = ftpClientProperties.toString();
        String pathName = "C:/Users/Public/Pictures/Sample Pictures/八仙花.jpg";
        File file = new File(pathName);
        ftpClientTemplate.uploadFile("/HOME/DIR","abc.txt",file);
        return config;
    }

    @RequestMapping("/testLang")
    @ResponseBody
    public String testI18n(){
        return "testLang";

    }

    @RequestMapping("/testMybatis")
    @ResponseBody
    public List testMybatis(){
        List list = userServiceImpl.selectUser();
        return list;
    }

    /**
     *
     * @return map
     */
    @RequestMapping("/insertUserRollback")
    @ResponseBody
    public Map<String,Object> insertUserRollback(){
        System.out.println("/insertUser");
        int r = userServiceImpl.insertUserRollback();
        Map<String,Object> map = new HashMap<>(16);
        map.put("code",r);
        return map;
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public Map<String,Object> insertUser(){
        int r = userServiceImpl.insertUser();
        Map<String,Object> map = new HashMap<>(16);
        map.put("code",r);
        return map;
    }

}
